home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5801 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  108 lines

  1. Path: news.magg.net!news
  2. From: n4mwd@magg.net (Dennis Hawkins)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: I need help on putting 2 strings together into 1(concatenate)
  5. Date: Wed, 21 Feb 1996 01:26:22 GMT
  6. Organization: M.A.G. Information Services (MAGG.NET)
  7. Message-ID: <4gdouc$p36@dopey.magg.net>
  8. References: <4gcr1b$ft3@cloner4.netcom.com>
  9. NNTP-Posting-Host: wpb-126.magg.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. spdcool@ix.netcom.com(SPD) wrote:
  13.  
  14. >  I figured out my earlier problem.  Thanks to all that helped.  Now I
  15. >have another problem.  First take a look at a short version of my code:
  16.  
  17. >#include <stdio.h>
  18. >#include <stdlib.h>
  19. >#include <string.h>
  20. >#include <time.h>
  21. >struct data {
  22. >    char *full_name;
  23.               ^^^^^^^^^^^   You are NOT defining storage, just a
  24. pointer.
  25. >    int age;
  26. >    float salary;
  27. > };
  28.  
  29. >struct name  {
  30. >   char *first1, *last1;
  31. >  };
  32.  
  33.  
  34. >int main(void)
  35. >{ 
  36. >  struct name fname[3], lname[3];
  37.     ^^^^^ since name is defined with both a pointer to first and last
  38. name, why do you need another fname and lname??? Why not just 
  39. struct name names[3];
  40. >  struct data secret[5];
  41. >  int i, ran;
  42.  
  43. >  fname[0].first1 = "Joe";
  44.                              ^^^^^^   This is not copying a string,
  45. only a pointer.  The space for "Joe" is auto-initialized with 4 bytes
  46. of storage.
  47. >  fname[1].first1 = "Michael";  <- 8 bytes
  48. >  fname[2].first1 = "Bruce";     <- 6 bytes
  49.  
  50. >  lname[0].last1 = "Jordan";    <- 7 bytes
  51. >  lname[1].last1 = "Willis";      <- 7 bytes
  52. >  lname[2].last1 = "Jackson";   <- 8 bytes
  53.  
  54. >  randomize()
  55. >  for (i = 0; i < 5; ++i)  {
  56. >   e = random(3);
  57. >   secret[i].full_name = fname[e].first1;/* the trouble some part begin
  58.             ^^^^^ Now points to the auto storage of at most 8 bytes
  59. (Michael).
  60. >   e = random(3);
  61. >   strcat(secret[i].full_name," ");     
  62.            ^^^^^^ overwrite the null terminator of the auto storage
  63. If full_name pointed to "Michael", now it points to "Michael Bruce"
  64. >   strcat(secret[i].full_name,lname[e].last1);  /*trouble ends here*/
  65.                                  ^^^^^  
  66. Assuming that the first name selected was Michael and the second was
  67. Jordan, you have now overwritten memory by 8 bytes " Jordan".  At
  68. best, this will result in some bizarre string outputs.  But most
  69. likely, your program will crash.
  70. >    "
  71. >     "
  72. >     "  /* the rest works  */
  73. >     "   }
  74. >   for (i = 0; i < 5; ++i)  {
  75. >    printf("%s%s",secret[i].full_name, "   ");
  76.       ^^^^ Whats the " " for?? What wrong with 
  77.       printf("%s ",secret[i].full_name);
  78. >    printf("%d%s%5.2f\n", secret[i].age,"    ",secret[i].salary);
  79.       ^^^^ More spaces, try:
  80.       printf("%d     %5.2f\n", secret[i].age,secret[i].salary);
  81. >    }
  82. >  return 0;
  83. >}
  84.  
  85.  
  86. Why not just try:
  87.  
  88.  
  89. for (i = 0; i < 5; ++i)  
  90. {
  91.      printf("%s %s",names[random(3)].first1, names[random(3)].last1);
  92. }
  93.  
  94. Your code was quite buggy.  You should consider using Turbo Debugger
  95. on this type of an app.  TD would have found the bug for you in much
  96. less time than it has taken me to write this note.  Also, you might
  97. consider using parallel arrays for the name storage.  It is much more
  98. efficient than using structures.  That way, all you would have to do
  99. would be auto-initialize the arrays and then:
  100.     printf("%s %s", first[random(3)], last[random(3)]);
  101.  
  102. Hope this helps.
  103.  
  104. Dennis Hawkins
  105. n4mwd@amsat.org
  106.  
  107.  
  108.